package com.example.elderlycare.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.elderlycare.dao.entity.EquipModel;
import com.example.elderlycare.dao.mapper.EquipModelMapper;
import com.example.elderlycare.service.EquipModelService;
import com.example.elderlycare.service.entity.EquipModelItem;
import com.example.elderlycare.service.entity.EquipModelListResponseBody;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class EquipModelServiceImpl extends ServiceImpl<EquipModelMapper, EquipModel> implements EquipModelService {
    @Resource
    EquipModelMapper equipModelMapper;

    /**
     * queryEquipModelList 根据参数查询列表
     *
     * @param current      当前页
     * @param pageSize     页面容量
     * @param queryWrapper 查询参数封装
     * @return EquipModelListResponseBody
     */
    @Override
    public EquipModelListResponseBody queryEquipModelList(int current, int pageSize, QueryWrapper<EquipModel> queryWrapper) {
        Page<EquipModel> page = new Page<>(current, pageSize);
        Page<EquipModel> equipModelPage = equipModelMapper.selectPage(page, queryWrapper);
        EquipModelListResponseBody.EquipModelListResponseBodyBuilder equipModelListResponseBodyBuilder = EquipModelListResponseBody.builder();
        equipModelListResponseBodyBuilder.total(Math.toIntExact(equipModelPage.getTotal()));
        List<EquipModelItem> items = new ArrayList<>();
        List<EquipModel> res = equipModelPage.getRecords();
        res.forEach((item) -> {
            EquipModelItem.EquipModelItemBuilder equipModelItemBuilder = EquipModelItem.builder().
                    id(item.getId()).
                    modelName(item.getModelName()).
                    equipType(item.getEquipType()).
                    supplier(item.getSupplier());
            items.add(equipModelItemBuilder.build());
        });
        equipModelListResponseBodyBuilder.items(items);
        return equipModelListResponseBodyBuilder.build();
    }
}
